package com.toxictest.testproject.core;

import static playn.core.PlayN.assets;
import static playn.core.PlayN.graphics;
import static playn.core.PlayN.log;
import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.ResourceCallback;

public class Shape {

	public static String IMAGE = "images/pea.png";
	private ImageLayer layer;
	private float[] location = { 0, 0 };
	private float angle;
	private float height;
	private float width;
	
	private static float SPEED_OF_FALL=3;
	
	private static float SPEED_OF_MOTION=15;

	public Shape(final GroupLayer shapeLayer) {
		Image image = assets().getImage(IMAGE);
		layer = graphics().createImageLayer(image);

		location[0] = graphics().width() / 2f;

		// Add a callback for when the image loads.
		// This is necessary because we can't use the width/height (to center
		// the
		// image) until after the image has been loaded
		image.addCallback(new ResourceCallback<Image>() {
			@Override
			public void done(Image image) {
				layer.setOrigin(image.width() / 2f, image.height() / 2f);
				setHeight(image.height());
				setWidth(image.width());
				location[1] = getHeight() / 2;
				layer.setTranslation(location[0], location[1]);
				shapeLayer.add(layer);
			}

			@Override
			public void error(Throwable err) {
				log().error("Error loading image!", err);
			}
		});
	}

	public void update() {
		location[1] += SPEED_OF_FALL;
		if (location[1] < (graphics().height() - getHeight() / 2))
			layer.setTranslation(location[0], location[1]);
		else
			layer.setTranslation(location[0],
					(graphics().height() - getHeight() / 2));
	}

	public void rotate() {
		angle += (float) java.lang.Math.PI / 2f;
		layer.setRotation(angle);
	}

	public float getHeight() {
		return height;
	}

	public void setHeight(float height) {
		this.height = height;
	}

	public float getLocationX() {
		return location[0];
	}

	public float getLocationY() {
		return location[1];
	}

	public float getWidth() {
		return width;
	}

	public void setWidth(float width) {
		this.width = width;
	}

	public void moveLeft() {
		
		location[0]-=SPEED_OF_MOTION;
		layer.setTranslation(location[0], location[1]);

	}

	public void moveRight() {
		
		location[0]+=SPEED_OF_MOTION;
		layer.setTranslation(location[0], location[1]);

	}

}
